package com.hxht.cmp.controller;

import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.pojo.SearchCriteria;
import com.hxht.cmp.service.JudgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/judge")
public class JudgeInformationController {
    @Autowired
    private JudgeService judgeService;

    /**
     * 根据多个法官Id查询案件排期或回顾
     *
     * @param searchCriteria
     * @return
     */
    @RequestMapping("/selectJudgeCasePlanOrReview")
    @ResponseBody
    public ResultResponse selectJudgeCasePlanOrReview(SearchCriteria searchCriteria) {
        if (ObjectUtils.isEmpty(searchCriteria)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        if (ObjectUtils.isEmpty(searchCriteria.getReview()) || ObjectUtils.isEmpty(searchCriteria.getStartDate())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        List<Map<String, Object>> mapList = judgeService.selectJudgeCasePlanOrReview(searchCriteria);
        return ResultResponse.success(mapList);
    }

    /**
     * 多法官的更多开庭和更多回顾
     *
     * @param searchCriteria
     * @return
     */
    @RequestMapping("/selectMoreCasePlanOrReview")
    public ResultResponse selectMoreCasePlanOrReview(SearchCriteria searchCriteria) {
        if (ObjectUtils.isEmpty(searchCriteria)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        if (ObjectUtils.isEmpty(searchCriteria.getReview())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        if (searchCriteria.getPage() < 1 || searchCriteria.getPageSize() < 1) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        Map<String, Object> map = judgeService.selectMoreCasePlanOrReview(searchCriteria);
        return ResultResponse.success(map);
    }


    /**
     * 法官个人开庭数，开庭总时长，开庭总容量
     * 默认查本年的
     *
     * @param userUids
     * @param courtroomUid
     * @return
     */
    @RequestMapping("/judgeStatistical")
    public ResultResponse selectJudgeStatistical(@RequestParam(value = "userUids[]") List<String> userUids, String courtroomUid) {
        if (userUids.isEmpty()) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        Map<String, Integer> map = new HashMap<>();
        //法官开庭数
        int judgeNumber = judgeService.getJudgeCourtStatistical(userUids);
        //法官开庭时长
        int judgeOpenTime = judgeService.getJudgeCourtTimeStatistical(userUids);
        //法官总容量
        int judgeFileSize = judgeService.getJudgeFileSizeStatistical(userUids, courtroomUid);
        map.put("judgeNumber", judgeNumber);
        map.put("judgeOpenTime", judgeOpenTime);
        map.put("judgeFileSize", judgeFileSize);
        return ResultResponse.success(map);
    }

    @RequestMapping("getJudgeCourtMonthStatistics")
    public ResultResponse getJudgeCourtMonthStatistics(@RequestParam(value = "judgeUids") List<String> judgeUids, String courtUid, String startDate, String endDate) {
        if ((judgeUids == null || judgeUids.size() == 0) && ObjectUtils.isEmpty(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(judgeService.getJudgeCourtMonthStatistics(judgeUids, courtUid, startDate, endDate));
    }

}
